﻿var prefix = "http://www.51rrkan.com/";
var preLen = prefix.length;
	document.addEventListener('DOMContentLoaded', function () {
		var searchVideoName = document.getElementById("searchVideoName");
		searchVideoName.addEventListener('click',clearContent);
		searchVideoName.addEventListener('keypress',keyPressHandler);
		
		var searchVideo = document.getElementById("searchVideo");
		searchVideo.addEventListener('click',openSearchResultPage);
		
	});
	
	/**
	    response Enter press
	*/
	function keyPressHandler() {
		if(event.keyCode==13) openSearchResultPage();
	}
	
	/**
		encode param and open target page
	*/
    function openSearchResultPage() {
		var searchBaseURL = "http://www.51rrkan.com/index.php?s=vod-search-wd-[name].html";
		var searchVideoName = document.getElementById("searchVideoName").value;
		if(searchVideoName != null && searchVideoName.length > 0) {
		    var url =  "http://www.51rrkan.com/index.php?s=vod-search-wd-" + encodeURIComponent(searchVideoName) + ".html"; 
			console.log("search target:" + url);
			window.open(url);
		}
	}
	function urlValidate(){
		var newAddress = document.getElementById("newAddress").value;
		if(newAddress.indexOf(prefix) == 0){
			$.ajax({
				url : "http://1.91testing.sinaapp.com/index.php",
				type: "post",
				dataType: "json",
				data:{
				    URL: newAddress
				},
				success:function(data, textStatus){
					$("#content").empty();
					if(data._ret == "success"){
						var newItem = createRecord(data._name, data._state, data._updateTime, data._url,data._type); 
						show();
					}else{
						return ;
					}
				}
			});
		}else{
			alert("请输入以 http://www.51rrkan.com/ 开头url");
			return ;
		}
	}
/*
	function createRecord(sName, sState, sDate, sLink,sType) {
		var Record = new Object();
		var key = "undefined";	
		
		if(sLink.indexOf(prefix) == 0) {
			Record.type = sType;
			Record.name = sName;
			Record.state = sState;
			Record.date = sDate;
			Record.link = sLink;
		
			
			key = sLink.substring(preLen, sLink.length);
		}
		
		if(key.lastIndexOf("/") == (key.length - 1)) 
			key = key.substring(0, key.length - 1);
		
		var infos = key.split("/");
		
		
		if(infos.length != 2) {
			return null;
		}

		key = infos[1];
		Record.key = key;
		var str = JSON.stringify(Record)
		
		localStorage[key] = str;
		var index = localStorage['index'];
		if(index != null && index.length > 0) {
			if(index.indexOf(key) == -1)
				index = index + "," + key;
		}
		else
			index = key;
		localStorage['index'] = index;
		return Record;
	}
*/
	
	function show() {
	    $("#content").empty();
		var index = localStorage['index'];
		console.log("index=" + index);
		if(index == null || index.length == 0) {
		    var $info = $("<span class='uname' style='font-style:italic'>暂无订阅视频</span>")
			$("#content").append($info)
		} else {
			var items = index.split(",");
			console.log("length:" + items.length);
			for(var i = 0 ; i < items.length; i++) {
				console.log(items[i] + ":" + localStorage[items[i]]);
		
				var data = localStorage[items[i]];
				var obj = JSON.parse(data);
				
				//console.log(obj.type);
				
				addNewRow(obj, (i + 1));
			}		
		}
	}
	
	$(function(){	
		show();
	});
	
	function addNewRow(record, index) {
	    var tempArr = record.link.split("/");
		var typeLink = tempArr[tempArr.length - 3];
		var typeLink = prefix + typeLink + "/";
	
		var $em = $('<em>' + index +'</em>');
		var $type = $('<a class="utype" href="'+ typeLink +'" target="_blank">' + record.type + '</a>');
		var $name = $('<a class="uname" href="' + record.link +'" target="_blank" title="' + record.name +'">' + record.name +'</a>');
		var $state = $('<span class="ustate">' + record.state + '</span>');
		var $date = $('<span class="umore"><font color="red">' + record.date +'</font></span>');
		var $cancel = $('<span class="ucancel" style="display:none"><a>取消订阅</a></span>');
		
		$cancel.click(function(){
		    deleteRecord(record.key);
			deleteVideoStaticInfo(record.key + "_detail");
			show();
		});
		
		var $li = $('<li id="' + record.key +'"></li>');
		$li.append($em);
		$li.append($type);
		$li.append($name);
		$li.append($state);
		$li.append($date);
		$li.append($cancel);
		
		$li.hover(function() {
			$("#" + record.key + " .ucancel").css("display", "");
			$("#" + record.key + " .umore").css("display", "none");
		}, function() {
			$("#" + record.key + " .ucancel").css("display", "none");
			$("#" + record.key + " .umore").css("display", "");
		});
		
		$("#content").append($li);
	}
	
	
	function clearContent() {
		document.getElementById("searchVideoName").value = "";
	}